package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.IProductDao;
import com.cn.wanxi.dao.impl.ProductDaoImpl;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import redis.clients.jedis.Jedis;
import sun.security.util.Length;

import java.util.ArrayList;
import java.util.List;

public class ProductServiceImpl implements IProductService {
    @Override
    public int add(ProductModel productModel) {
        IProductDao iProductDao=new ProductDaoImpl();
        int count=iProductDao.add(productModel);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("product_id");
            jedis.del("product_imgHref");
            jedis.del("product_content");
            jedis.del("product_casetypename");
        }
        return count;
    }

    @Override
    public List<ProductModel> findAll(PageModel pageModel) {
        IProductDao iProductDao=new ProductDaoImpl();
        List<ProductModel> list=new ArrayList<>();
        Jedis jedis=new Jedis();
        Long length=jedis.llen("product_id");
        if (pageModel.getPageSize()!=10 || pageModel.getPageNum()!=1){//注意整形用=号
            list=iProductDao.findAll(pageModel);
        }else  if(length>0){
            List<String> id=jedis.lrange("product_id",0,length);
            List<String> imgHref=jedis.lrange("product_imgHref",0,length);
            List<String> content=jedis.lrange("product_content",0,length);
            List<String> casetypename=jedis.lrange("product_casetypename",0,length);
            for (int i=0;i<length;i++){
                ProductModel model=new ProductModel();
                model.setId(Integer.parseInt(id.get(i)));
                model.setImgHref(imgHref.get(i));
                model.setContent(content.get(i));
                model.setCasetypename(casetypename.get(i));
                list.add(model);
            }
        }else {
            list=iProductDao.findAll(pageModel);
            for (ProductModel model:list) {
                jedis.rpush("product_id", model.getId().toString());
                jedis.rpush("product_imgHref",model.getImgHref());
                jedis.rpush("product_content",model.getContent());
                jedis.rpush("product_casetypename",model.getCasetypename());
            }
        }
        return list;
    }

    @Override
    public List<ProductModel> productFind() {
        IProductDao iProductDao=new ProductDaoImpl();
        List<ProductModel> list=new ArrayList<>();
        Jedis jedis=new Jedis();
        Long length=jedis.llen("product_id");
        if(length>0){
            List<String> id=jedis.lrange("product_id",0,length);
            List<String> imgHref=jedis.lrange("product_imgHref",0,length);
            List<String> content=jedis.lrange("product_content",0,length);
            List<String> casetypename=jedis.lrange("product_casetypename",0,length);
            for (int i=0;i<length;i++){
                ProductModel model=new ProductModel();
                model.setId(Integer.parseInt(id.get(i)));
                model.setImgHref(imgHref.get(i));
                model.setContent(content.get(i));
                model.setCasetypename(casetypename.get(i));
                list.add(model);
            }
        }else {
            list=iProductDao.productFind();
            for (ProductModel model:list) {
                jedis.rpush("product_id", model.getId().toString());
                jedis.rpush("product_imgHref",model.getImgHref());
                jedis.rpush("product_content",model.getContent());
                jedis.rpush("product_casetypename",model.getCasetypename());
            }
        }
        return list;
    }

    @Override
    public int deletebyid(int aa) {
        IProductDao iProductDao=new ProductDaoImpl();
        int count= iProductDao.deletebyid(aa);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("product_id");
            jedis.del("product_imgHref");
            jedis.del("product_content");
            jedis.del("product_casetypename");
        }
        return count;
    }

    @Override
    public ProductModel findbyId(int productid) {
        IProductDao iProductDao=new ProductDaoImpl();
        ProductModel productModel=iProductDao.findbyId(productid);
        CaseTypeServiceImpl caseTypeService=new CaseTypeServiceImpl();
        productModel.setList(caseTypeService.CaseTypeFindAll());
        return productModel;
    }

    @Override
    public int update(ProductModel productModel) {
        IProductDao iProductDao=new ProductDaoImpl();
        int count= iProductDao.update(productModel);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("product_id");
            jedis.del("product_imgHref");
            jedis.del("product_content");
            jedis.del("product_casetypename");
        }
        return count;
    }

    @Override
    public int getCount(ProductModel productModel) {
        IProductDao iProductDao=new ProductDaoImpl();
        return iProductDao.getCount(productModel);
    }

    @Override
    public List<ProductModel> productTh(ProductModel productModel) {
        IProductDao iProductDao=new ProductDaoImpl();
        List<ProductModel> list=iProductDao.productTh(productModel);
        return list;
    }

}











